From top to bottom, the code is executed by the Java compiler. The order in which the statements appear in the code determines how they are executed. To control the flow of Java code, however, statements are offered by Java. Control flow statements are what these sentences are known as. It is one of the core characteristics of Java that ensures a seamless flow of programs.
1. Decision Making statements
2. Loop statements
3. Jump statements
In this statement, the if block is only performed if the provided condition is true. If not, the else block is carried out.
// Decision-Making program using if...else
import java.io.*;
public class Control{
public static void main(String args[ ]){
int x = 8;
if (x>10)
System.out.println("X is greater than 10");
else
System.out.println("X is less than 10");
}
}
Output:
X is less than 10
The four different kinds of if-statements are listed below.
Let's look at each of the if statements one by one.
Syntax
if (condition) {
statement 1; // your code execute when condition is true
}
// Decision-Making program using if
import java.io.*;
public class Control{
public static void main(String args[ ]){
int x = 7;
int y = 4;
if (x-y>2)
System.out.println( "x-y is greater than 2");
}
}
Output:
x-y is greater than 2
Syntax
if (condition) {
statement 1; // your code execute when condition is true
}
else {
statement 2; // your code execute when condition is false
}
// Decision-Making program using if...else
import java.io.*;
public class Control{
public static void main(String args[ ]){
int x = 8;
int y = 10;
if (x-y>5)
System.out.println( "x-y is greater than 5");
else
System.out.println("X is less than 5");
}
}
Output:
X is less than 5
Tip
if one statement is inside if and else no need to open curley barcket if there is multiple statements then you must use curley bracket to execute that block of code inside it.
Let's see one example
// Decision-Making program using if...else
import java.io.*;
public class Control{
public static void main(String args[ ]){
int x = 11;
if (x==10)
System.out.println("x is equal to 10"); //only one statement inside if
if (x>10) {
System.out.println("x is greater than 10"); //multiple statement inside if
System.out.println("value of x is "+x);
}
else
System.out.println("X is less than 10");
}
}
Output:
x is greater than 10
value of x is 11
The if-statement is followed by several else-if statements in an if-else-if statement. In other terms, we may say that a decision tree is created by the series of if-else statements, and the computer can enter the block of code when the condition is true. At the last, we may also define an else statement.
Syntax
if (condition1) {
statement 1; // your code execute when condition 1 is true
}
else if (condition2) {
statement 2; // your code execute when condition 2 is true
}
else {
statement 3; // your code execute when condition is false
}
// Decision-Making program using if - else if
import java.io.*;
public class Control{
public static void main(String args[ ]){
String Country = "India";
if (Country== "USA") {
System.out.println("Welcome to USA");
}
else if (Country=="India") {
System.out.println("Welcome to India");
}
else if (Country=="Japan") {
System.out.println("Welcome to Japan");
}
else
System.out.println("Welcome to"+Country);
}
}
Output:
Welcome to India
If or if-else statements may be contained within another if or else-if statement it is called nested if-statements.
Syntax
if (condition1) {
if (condition1) {
statement 1; // your code execute when condition 2 is true
}
else {
statement 2; // your code execute when condition is false
}
}
// Decision-Making program using nested if - else if
class Student { public static void main(String args[]) { int a=5; int b=10; if(a==5) { if(b==10) { System.out.println("Welcome to my website"); } else { System.out.println("Sorry value of b is not 10"); } } } }
Output:
Welcome to my website
Switch statement
Syntax
switch (expression){
case value1:
statement 1; // your code execute when condition 2 is true
break;
case value2:
statement 2; // your code execute when condition 2 is true
break;
default:
statement 2; // your code execute when condition is false
}
}
// Decision-Making program using switch case statement
public class Student { public static void main(String[] args) { int days = 5; switch (days) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; } } }
Output:
Friday
With only one line of code, we can initialise the loop variable, verify the condition, and increment or decrement. Only when we are certain of the number of times we want to run the block of code do we utilise the for loop.
Syntax
for (initialization, condition, increment/decrement) {
// block of statement
}
// program using java for loop
public class Student { public static void main(String[] args) { int sum = 0; for(int j = 0; j<=100; j++) { sum = sum + j; } System.out.println("The sum of first 100 whole numbers is " + sum); } }
Output:
The sum of first 100 whole numbers is 5050
With only one line of code, we can initialise the loop variable, verify the condition, and increment or decrement. Only when we are certain of the number of times we want to run the block of code do we utilise the for loop.
Syntax
for (String string : args) {
// block of statement
}
// Java Loop program using for-each loop
public class Student { public static void main(String[] args) { String[] Country = {"India","USA","Canada","Japan","SriLanka"}; for(String CountryList:Country) { System.out.println(CountryList); } } }
Output:
India
USA
Canada
Japan
SriLanka
The while loop is also used to repeatedly loop through the number of statements. However, it is advised to use a while loop if we are unsure of the number of iterations. In contrast to for loop, while loop's initialization and increment/decrement operations do not happen inside the while loop statement.
Since the condition is verified at the beginning of the loop, it is also referred to as the entry-controlled loop. If the condition is satisfied, the body of the loop will be run; otherwise, the statements following the loop will be run.
Syntax
while (condition) {
// block of statement
}
// Java Loop program using while loop
public class Student { public static void main(String[] args) { int i = 1; System.out.println("Print the first 10 natural numbers "); while(i<=10) { System.out.println(i); i = i + 1; } } }
Output:
Print the first 10 natural numbers
1
2
3
4
5
6
7
8
9
10
A block of statements is constantly run until the specified condition is true using a Java do-while loop. The Java do-while loop is similar to the while loop with the exception that the condition is verified after the statements have been executed. do..while loop will ensure that statement inside loop will execute once before condition verification
Syntax
do {
// block of statement
}while(condition);
// Java Loop program using do.. while loop
public class Student { public static void main(String[] args) { int i = 1; System.out.println("Printing the list of first 10 odd numbers"); do { System.out.println(i); i = i + 2; }while(i<=10); } }
Output:
Printing the list of first 10 odd numbers
1
3
5
7
9
Jump statements are control statements that move programme execution from one location execution flow to another. In the Java programming language, there are two Jump statements available. i.e break & continue
1. Break statement.
2. Continue statement
The break statement in Java is used to stop the execution of the looping or switch statement that is closest. With the switch statement, for loop, while loop, and do-while loop, the break statement is frequently used.
Syntax
break;
When a loop encounters a break statement, the loop is broken, and control shifts to the sentence that follows the loop.
// break statement inside loop
public class Student { public static void main(String[] args) { for(int i = 1; i<= 20; i++) { System.out.println(i); if(i==9) { break; // loop will stop at 9 } } } }
Output:
1
2
3
4
5
6
7
8
9
The continue statement skips any code in between itself and the conditional expression that governs the loop, causing the next iteration of the loop to execute.
Syntax
continue;
When a loop encounters a continue statement, that iteration is skip in loop
// continue statement inside loop
public class Student { public static void main(String[] args) { for (int i = 1; i < 15; i++) { if (i == 9){ System.out.println(); // this print statement to just show skip value continue; // use continue keyword to skip the current iteration } System.out.println(i); } } }
Output:
1
2
3
4
5
6
7
8
10
11
12
13
14
Post your comment